var favoriteLocationsCache = [];
var containerHTML = "";

function hideTree(link)
{
	$(link).parents('tr').css('display','none');
	containerHTML = $("#externalBody").html();
}

function travelToLink(x, y, link)
{
	$(link).css("color", "purple");
	goToLatLng(x,y);
	containerHTML = $("#externalBody").html();
}

function doAjax(url,container){
	// if the URL starts with http
	if(url.match('^http')){
		// assemble the YQL call
		$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
				"q=select%20*%20from%20html%20where%20url%3D%22"+
				encodeURIComponent(url)+
				"%22&format=xml'&callback=?",
			function(data){
				if(data.results[0]){
					var data = filterData(data.results[0]);
					container.html(data);
				} else {
					var errormsg = '<p>Error: could not load the page.</p>';
					container.html(errormsg);
          		}
//           		container.find("td:contains('yes')").parent().addClass("maxedTree").css({"font-size":"10px", "display":"none"});
//           		container.find("tr:gt(0)").not(".maxedTree").css({"background":"#ccffbb","font-size":"14px"});
//           		container.find("tr:gt(0)").append('<td><a href="#" onclick="hideTree(this);">hide</a></td>');
//           		container.find("tr:lt(1)").append('<td><a href="javascript:$(\'.maxedTree\').css(\'display\',\'\');">Show Maxed</a></td>');
        	}
		);
    } else {
      $.ajax({
        url: url,
        timeout:5000,
        success: function(data){
          container.html(data);
        },
        error: function(req,error){
          if(error === 'error'){error = req.statusText;}
          var errormsg = 'There was a communication error: '+error;
            container.html(errormsg);
        }
      });
    }
  }
  function filterData(data){
    // filter all the nasties out
    // no body tags
    data = data.replace(/<?\/body[^>]*>/g,'');
    // no linebreaks
    data = data.replace(/[\r|\n]+/g,'');
    // no comments
    data = data.replace(/<--[\S\s]*?-->/g,'');
    // no noscript blocks
    data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,'');
    // no script blocks
    data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,'');
    // no self closing scripts
    data = data.replace(/<script.*\/>/,'');
    // [... add as needed ...]
    data = data.replace(/(loc:(-?\d+\.?\d*),\s?(-?\d+\.?\d*))/g, '<a class="locationLink" onclick="travelToLink($2, $3, this);">$1</a>');
    return data;
  }
  
  function popupExternalWindow(content){
	var contentButton = $('<div class="button small"><span id="contentPopupButton">Wishing Wells</span></div>');
	contentButton.click(
		function(){
			$('<div class="popup blue large" id="popupFrame_external"><div class="closeFrameButton" onclick="$(this).parent().remove();"><img class="closeFrameButtonImg" src="/htmlui/images/icons/sprite-icons-shared3.png"></div><div class="popupFrameTitle">External Content</div><div id="externalBody" class="popupFrameBody"></div></div>')
				.appendTo("#contentBody").draggable({handle: ".popupFrameTitle", stack: ".popup"});
			var container = $("#externalBody").css({"overflow-y": "auto", "overflow-x": "hidden"});
			//container.append('<iframe src="http://waodds.servegame.org/xmas2011/Table.htm" style="width: 717px; height: 352px;"></iframe>');
			
			/*Non-Holiday Behavior*/
			containerHTML ? $("#externalBody").html(containerHTML) : doAjax("http://jsommers.com/WishingWells.html" , container);
			
			/*Holiday Specific External Content*/
			//containerHTML ? $("#externalBody").html(containerHTML) : doAjax("http://fleck.yi.org/corn2012/corn.htm" , container);
			//containerHTML ? $("#externalBody").html(containerHTML) : doAjax("http://fleck.yi.org/xmas2012/table.htm" , container);
			
			containerHTML = $("#externalBody").html();
		});
	$("#footer").before(contentButton);
	//var holidayButton = $('<div class="button small"><span id="contentHolidayPopupButton">Cornucopias</span></div>');
}

$(document).ready(function(){
	popupExternalWindow(null);
});